home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / rule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-09  |  15.7 KB  |  590 lines

  1. /* Pattern and suffix rule internals for GNU Make.
  2. Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "filedef.h"
  22. #include "job.h"
  23. #include "commands.h"
  24. #include "variable.h"
  25. #include "rule.h"
  26.  
  27. static void freerule PARAMS ((struct rule *rule, struct rule *lastrule));
  28.  
  29. /* Chain of all pattern rules.  */
  30.  
  31. struct rule *pattern_rules;
  32.  
  33. /* Pointer to last rule in the chain, so we can add onto the end.  */
  34.  
  35. struct rule *last_pattern_rule;
  36.  
  37. /* Number of rules in the chain.  */
  38.  
  39. unsigned int num_pattern_rules;
  40.  
  41. /* Maximum number of target patterns of any pattern rule.  */
  42.  
  43. unsigned int max_pattern_targets;
  44.  
  45. /* Maximum number of dependencies of any pattern rule.  */
  46.  
  47. unsigned int max_pattern_deps;
  48.  
  49. /* Maximum length of the name of a dependencies of any pattern rule.  */
  50.  
  51. unsigned int max_pattern_dep_length;
  52.  
  53. /* Pointer to structure for the file .SUFFIXES
  54.    whose dependencies are the suffixes to be searched.  */
  55.  
  56. struct file *suffix_file;
  57.  
  58. /* Maximum length of a suffix.  */
  59.  
  60. unsigned int maxsuffix;
  61.  
  62. /* Compute the maximum dependency length and maximum number of
  63.    dependencies of all implicit rules.  Also sets the subdir
  64.    flag for a rule when appropriate, possibly removing the rule
  65.    completely when appropriate.  */
  66.  
  67. void
  68. count_implicit_rule_limits ()
  69. {
  70.   char *name;
  71.   unsigned int namelen;
  72.   register struct rule *rule, *lastrule;
  73.  
  74.   num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
  75.   max_pattern_dep_length = 0;
  76.  
  77.   name = 0;
  78.   namelen = 0;
  79.   rule = pattern_rules;
  80.   lastrule = 0;
  81.   while (rule != 0)
  82.     {
  83.       unsigned int ndeps = 0;
  84.       register struct dep *dep;
  85.       struct rule *next = rule->next;
  86.       unsigned int ntargets;
  87.  
  88.       ++num_pattern_rules;
  89.       
  90.       ntargets = 0;
  91.       while (rule->targets[ntargets] != 0)
  92.     ++ntargets;
  93.  
  94.       if (ntargets > max_pattern_targets)
  95.     max_pattern_targets = ntargets;
  96.  
  97.       for (dep = rule->deps; dep != 0; dep = dep->next)
  98.     {
  99.       unsigned int len = strlen (dep->name);
  100.  
  101. #ifdef VMS
  102.       char *p = rindex (dep->name, ']');
  103. #else
  104.       char *p = rindex (dep->name, '/');
  105. #endif
  106.       char *p2 = p != 0 ? index (dep->name, '%') : 0;
  107.       ndeps++;
  108.  
  109.       if (len > max_pattern_dep_length)
  110.         max_pattern_dep_length = len;
  111.  
  112.       if (p != 0 && p2 > p)
  113.         {
  114.           /* There is a slash before the % in the dep name.
  115.          Extract the directory name.  */
  116.           if (p == dep->name)
  117.         ++p;
  118.           if (p - dep->name > namelen)
  119.         {
  120.           if (name != 0)
  121.             free (name);
  122.           namelen = p - dep->name;
  123.           name = (char *) xmalloc (namelen + 1);
  124.         }
  125.           bcopy (dep->name, name, p - dep->name);
  126.           name[p - dep->name] = '\0';
  127.  
  128.           /* In the deps of an implicit rule the `changed' flag
  129.          actually indicates that the dependency is in a
  130.          nonexistent subdirectory.  */
  131.  
  132.           dep->changed = !dir_file_exists_p (name, "");
  133. #ifdef VMS
  134.           if (dep->changed && *name == ']')
  135. #else
  136.           if (dep->changed && *name == '/')
  137. #endif
  138.         {
  139.           /* The name is absolute and the directory does not exist.
  140.              This rule can never possibly match, since this dependency
  141.              can never possibly exist.  So just remove the rule from
  142.              the list.  */
  143.           freerule (rule, lastrule);
  144.           --num_pattern_rules;
  145.           goto end_main_loop;
  146.         }
  147.         }
  148.       else
  149.         /* This dependency does not reside in a subdirectory.  */
  150.         dep->changed = 0;
  151.     }
  152.  
  153.       if (ndeps > max_pattern_deps)
  154.     max_pattern_deps = ndeps;
  155.  
  156.       lastrule = rule;
  157.     end_main_loop:
  158.       rule = next;
  159.     }
  160.   
  161.   if (name != 0)
  162.     free (name);
  163. }
  164.  
  165. /* Create a pattern rule from a suffix rule.
  166.    TARGET is the target suffix; SOURCE is the source suffix.
  167.    CMDS are the commands.
  168.    If TARGET is nil, it means the target pattern should be `(%.o)'.
  169.    If SOURCE is nil, it means there should be no deps.  */
  170.  
  171. static void
  172. convert_suffix_rule (target, source, cmds)
  173.      char *target, *source;
  174.      struct commands *cmds;
  175. {
  176.   char *targname, *targpercent, *depname;
  177.   char **names, **percents;
  178.   struct dep *deps;
  179.   unsigned int len;
  180.  
  181.   if (target == 0)
  182.     /* Special case: TARGET being nil means we are defining a
  183.        `.X.a' suffix rule; the target pattern is always `(%.o)'.  */
  184.     {
  185. #ifdef VMS
  186.       targname = savestring ("(%.obj)", 7);
  187. #else
  188.       targname = savestring ("(%.o)", 5);
  189. #endif
  190.       targpercent = targname + 1;
  191.     }
  192.   else
  193.     {
  194.       /* Construct the target name.  */
  195.       len = strlen (target);
  196.       targname = xmalloc (1 + len + 1);
  197.       targname[0] = '%';
  198.       bcopy (target, targname + 1, len + 1);
  199.       targpercent = targname;
  200.     }
  201.  
  202.   names = (char **) xmalloc (2 * sizeof (char *));
  203.   percents = (char **) alloca (2 * sizeof (char *));
  204.   names[0] = targname;
  205.   percents[0] = targpercent;
  206.   names[1] = percents[1] = 0;
  207.  
  208.   if (source == 0)
  209.     deps = 0;
  210.   else
  211.     {
  212.       /* Construct the dependency name.  */
  213.       len = strlen (source);
  214.       depname = xmalloc (1 + len + 1);
  215.       depname[0] = '%';
  216.       bcopy (source, depname + 1, len + 1);
  217.       deps = (struct dep *) xmalloc (sizeof (struct dep));
  218.       deps->next = 0;
  219.       deps->name = depname;
  220.     }
  221.  
  222.   create_pattern_rule (names, percents, 0, deps, cmds, 0);
  223. }
  224.  
  225. /* Convert old-style suffix rules to pattern rules.
  226.    All rules for the suffixes on the .SUFFIXES list
  227.    are converted and added to the chain of pattern rules.  */
  228.  
  229. void
  230. convert_to_pattern ()
  231. {
  232.   register struct dep *d, *d2;
  233.   register struct file *f;
  234.   register char *rulename;
  235.   register unsigned int slen, s2len;
  236.  
  237.   /* Compute maximum length of all the suffixes.  */
  238.  
  239.   maxsuffix = 0;
  240.   for (d = suffix_file->deps; d != 0; d = d->next)
  241.     {
  242.       register unsigned int namelen = strlen (dep_name (d));
  243.       if (namelen > maxsuffix)
  244.     maxsuffix = namelen;
  245.     }
  246.  
  247.   rulename = (char *) alloca ((maxsuffix * 2) + 1);
  248.  
  249.   for (d = suffix_file->deps; d != 0; d = d->next)
  250.     {
  251.       /* Make a rule that is just the suffix, with no deps or commands.
  252.      This rule exists solely to disqualify match-anything rules.  */
  253.       convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
  254.  
  255.       f = d->file;
  256.       if (f->cmds != 0)
  257.     /* Record a pattern for this suffix's null-suffix rule.  */
  258.     convert_suffix_rule ("", dep_name (d), f->cmds);
  259.  
  260.       /* Record a pattern for each of this suffix's two-suffix rules.  */
  261.       slen = strlen (dep_name (d));
  262.       bcopy (dep_name (d), rulename, slen);
  263.       for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  264.     {
  265.       s2len = strlen (dep_name (d2));
  266.  
  267.       if (slen == s2len && streq (dep_name (d), dep_name (d2)))
  268.         continue;
  269.  
  270.       bcopy (dep_name (d2), rulename + slen, s2len + 1);
  271.       f = lookup_file (rulename);
  272.       if (f == 0 || f->cmds == 0)
  273.         continue;
  274.  
  275.       if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a')
  276.         /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
  277.            It also generates a normal `%.a: %.X' rule below.  */
  278.         convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'.  */
  279.                  dep_name (d),
  280.                  f->cmds);
  281.  
  282.       /* The suffix rule `.X.Y:' is converted
  283.          to the pattern rule `%.Y: %.X'.  */
  284.       convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds);
  285.     }
  286.     }
  287. }
  288.  
  289.  
  290. /* Install the pattern rule RULE (whose fields have been filled in)
  291.    at the end of the list (so that any rules previously defined
  292.    will take precedence).  If this rule duplicates a previous one
  293.    (identical target and dependencies), the old one is replaced
  294.    if OVERRIDE is nonzero, otherwise this new one is thrown out.
  295.    When an old rule is replaced, the new one is put at the end of the
  296.    list.  Return nonzero if RULE is used; zero if not.  */
  297.  
  298. int
  299. new_pattern_rule (rule, override)
  300.      register struct rule *rule;
  301.      int override;
  302. {
  303.   register struct rule *r, *lastrule;
  304.   register unsigned int i, j;
  305.  
  306.   rule->in_use = 0;
  307.   rule->terminal = 0;
  308.  
  309.   rule->next = 0;
  310.  
  311.   /* Search for an identical rule.  */
  312.   lastrule = 0;
  313.   for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
  314.     for (i = 0; rule->targets[i] != 0; ++i)
  315.       {
  316.     for (j = 0; r->targets[j] != 0; ++j)
  317.       if (!streq (rule->targets[i], r->targets[j]))
  318.         break;
  319.     if (r->targets[j] == 0)
  320.       /* All the targets matched.  */
  321.       {
  322.         register struct dep *d, *d2;
  323.         for (d = rule->deps, d2 = r->deps;
  324.          d != 0 && d2 != 0; d = d->next, d2 = d2->next)
  325.           if (!streq (dep_name (d), dep_name (d2)))
  326.         break;
  327.         if (d == 0 && d2 == 0)
  328.           /* All the dependencies matched.  */
  329.           if (override)
  330.         {
  331.           /* Remove the old rule.  */
  332.           freerule (r, lastrule);
  333.           /* Install the new one.  */
  334.           if (pattern_rules == 0)
  335.             pattern_rules = rule;
  336.           else
  337.             last_pattern_rule->next = rule;
  338.           last_pattern_rule = rule;
  339.           
  340.           /* We got one.  Stop looking.  */
  341.           goto matched;
  342.         }
  343.           else
  344.         {
  345.           /* The old rule stays intact.  Destroy the new one.  */
  346.           freerule (rule, (struct rule *) 0);
  347.           return 0;
  348.         }
  349.       }
  350.       }
  351.  
  352.  matched:;
  353.  
  354.   if (r == 0)
  355.     {
  356.       /* There was no rule to replace.  */
  357.       if (pattern_rules == 0)
  358.     pattern_rules = rule;
  359.       else
  360.     last_pattern_rule->next = rule;
  361.       last_pattern_rule = rule;
  362.     }
  363.  
  364.   return 1;
  365. }
  366.  
  367.  
  368. /* Install an implicit pattern rule based on the three text strings
  369.    in the structure P points to.  These strings come from one of
  370.    the arrays of default implicit pattern rules.
  371.    TERMINAL specifies what the `terminal' field of the rule should be.  */
  372.  
  373. void
  374. install_pattern_rule (p, terminal)
  375.      struct pspec *p;
  376.      int terminal;
  377. {
  378.   register struct rule *r;
  379.   char *ptr;
  380.  
  381.   r = (struct rule *) xmalloc (sizeof (struct rule));
  382.  
  383.   r->targets = (char **) xmalloc (2 * sizeof (char *));
  384.   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
  385.   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
  386.  
  387.   r->targets[1] = 0;
  388.   r->suffixes[1] = 0;
  389.   r->lens[1] = 0;
  390.  
  391.   r->lens[0] = strlen (p->target);
  392.   /* These will all be string literals, but we malloc space for
  393.      them anyway because somebody might want to free them later on.  */
  394.   r->targets[0] = savestring (p->target, r->lens[0]);
  395.   r->suffixes[0] = find_percent (r->targets[0]);
  396.   if (r->suffixes[0] == 0)
  397.     /* Programmer-out-to-lunch error.  */
  398.     abort ();
  399.   else
  400.     ++r->suffixes[0];
  401.  
  402.   ptr = p->dep;
  403.   r->deps = (struct dep *) multi_glob (parse_file_seq (&ptr, '\0',
  404.                                                        sizeof (struct dep), 1),
  405.                        sizeof (struct dep));
  406.  
  407.   if (new_pattern_rule (r, 0))
  408.     {
  409.       r->terminal = terminal;
  410.       r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
  411.       r->cmds->filename = 0;
  412.       r->cmds->lineno = 0;
  413.       /* These will all be string literals, but we malloc space for them
  414.      anyway because somebody might want to free them later.  */
  415.       r->cmds->commands = savestring (p->commands, strlen (p->commands));
  416.       r->cmds->command_lines = 0;
  417.     }
  418. }
  419.  
  420.  
  421. /* Free all the storage used in RULE and take it out of the
  422.    pattern_rules chain.  LASTRULE is the rule whose next pointer
  423.    points to RULE.  */
  424.  
  425. static void
  426. freerule (rule, lastrule)
  427.      register struct rule *rule, *lastrule;
  428. {
  429.   struct rule *next = rule->next;
  430.   register unsigned int i;
  431.  
  432.   for (i = 0; rule->targets[i] != 0; ++i)
  433.     free (rule->targets[i]);
  434.  
  435.   free ((char *) rule->targets);
  436.   free ((char *) rule->suffixes);
  437.   free ((char *) rule->lens);
  438.  
  439.   /* We can't free the storage for the commands because there
  440.      are ways that they could be in more than one place:
  441.        * If the commands came from a suffix rule, they could also be in
  442.        the `struct file's for other suffix rules or plain targets given
  443.        on the same makefile line.
  444.        * If two suffixes that together make a two-suffix rule were each
  445.        given twice in the .SUFFIXES list, and in the proper order, two
  446.        identical pattern rules would be created and the second one would
  447.        be discarded here, but both would contain the same `struct commands'
  448.        pointer from the `struct file' for the suffix rule.  */
  449.  
  450.   free ((char *) rule);
  451.  
  452.   if (pattern_rules == rule)
  453.     if (lastrule != 0)
  454.       abort ();
  455.     else
  456.       pattern_rules = next;
  457.   else if (lastrule != 0)
  458.     lastrule->next = next;
  459.   if (last_pattern_rule == rule)
  460.     last_pattern_rule = lastrule;
  461. }
  462.  
  463. /* Create a new pattern rule with the targets in the nil-terminated
  464.    array TARGETS.  If TARGET_PERCENTS is not nil, it is an array of
  465.    pointers into the elements of TARGETS, where the `%'s are.
  466.    The new rule has dependencies DEPS and commands from COMMANDS.
  467.    It is a terminal rule if TERMINAL is nonzero.  This rule overrides
  468.    identical rules with different commands if OVERRIDE is nonzero.
  469.  
  470.    The storage for TARGETS and its elements is used and must not be freed
  471.    until the rule is destroyed.  The storage for TARGET_PERCENTS is not used;
  472.    it may be freed.  */
  473.  
  474. void
  475. create_pattern_rule (targets, target_percents,
  476.              terminal, deps, commands, override)
  477.      char **targets, **target_percents;
  478.      int terminal;
  479.      struct dep *deps;
  480.      struct commands *commands;
  481.      int override;
  482. {
  483.   register struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
  484.   register unsigned int max_targets, i;
  485.  
  486.   r->cmds = commands;
  487.   r->deps = deps;
  488.   r->targets = targets;
  489.  
  490.   max_targets = 2;
  491.   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
  492.   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
  493.   for (i = 0; targets[i] != 0; ++i)
  494.     {
  495.       if (i == max_targets - 1)
  496.     {
  497.       max_targets += 5;
  498.       r->lens = (unsigned int *)
  499.         xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
  500.       r->suffixes = (char **)
  501.         xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
  502.     }
  503.       r->lens[i] = strlen (targets[i]);
  504.       r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
  505.             : target_percents[i]) + 1;
  506.       if (r->suffixes[i] == 0)
  507.     abort ();
  508.     }
  509.  
  510.   if (i < max_targets - 1)
  511.     {
  512.       r->lens = (unsigned int *) xrealloc ((char *) r->lens,
  513.                        (i + 1) * sizeof (unsigned int));
  514.       r->suffixes = (char **) xrealloc ((char *) r->suffixes,
  515.                     (i + 1) * sizeof (char *));
  516.     }
  517.  
  518.   if (new_pattern_rule (r, override))
  519.     r->terminal = terminal;
  520. }
  521.  
  522. /* Print the data base of rules.  */
  523.  
  524. static void            /* Useful to call from gdb.  */
  525. print_rule (r)
  526.      struct rule *r;
  527. {
  528.   register unsigned int i;
  529.   register struct dep *d;
  530.  
  531.   for (i = 0; r->targets[i] != 0; ++i)
  532.     {
  533.       fputs (r->targets[i], stdout);
  534.       if (r->targets[i + 1] != 0)
  535.     putchar (' ');
  536.       else
  537.     putchar (':');
  538.     }
  539.   if (r->terminal)
  540.     putchar (':');
  541.  
  542.   for (d = r->deps; d != 0; d = d->next)
  543.     printf (" %s", dep_name (d));
  544.   putchar ('\n');
  545.  
  546.   if (r->cmds != 0)
  547.     print_commands (r->cmds);
  548. }
  549.  
  550. void
  551. print_rule_data_base ()
  552. {
  553.   register unsigned int rules, terminal;
  554.   register struct rule *r;
  555.  
  556.   puts ("\n# Implicit Rules");
  557.  
  558.   rules = terminal = 0;
  559.   for (r = pattern_rules; r != 0; r = r->next)
  560.     {
  561.       ++rules;
  562.  
  563.       putchar ('\n');
  564.       print_rule (r);
  565.  
  566.       if (r->terminal)
  567.     ++terminal;
  568.     }
  569.  
  570.   if (rules == 0)
  571.     puts ("\n# No implicit rules.");
  572.   else
  573.     {
  574.       printf ("\n# %u implicit rules, %u", rules, terminal);
  575. #ifndef    NO_FLOAT
  576.       printf (" (%.1f%%)", (double) terminal / (double) rules * 100.0);
  577. #else
  578.       {
  579.     int f = (terminal * 1000 + 5) / rules;
  580.     printf (" (%d.%d%%)", f/10, f%10);
  581.       }
  582. #endif
  583.       puts (" terminal.");
  584.     }
  585.  
  586.   if (num_pattern_rules != rules)
  587.     fatal ("BUG: num_pattern_rules wrong!  %u != %u",
  588.        num_pattern_rules, rules);
  589. }
  590.